package pck_gui;

//import java.sql.Connection;
//import java.sql.ResultSet;
//import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import pck_pojos.Cls_Profesion;
//import pck_utilitarios.Cls_Conexion;
import pck_utilitarios.Cls_Funciones;
import pck_utilitarios.Cls_InvocaWebService;
import pck_utilitarios.Cls_Parametros;
import pck_utilitarios.JP_Modelo;

/**
 * @author supermiguelon
 */
public class JP_Profesiones_Buscar extends JP_Modelo {

    /** Creates new form JP_Profesiones_Buscar */
    public JP_Profesiones_Buscar() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        TXT_Profesion = new javax.swing.JTextField();
        BTN_Buscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TBL_Profesiones_Buscar = new javax.swing.JTable();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(udprrhh.UDPRRHHApp.class).getContext().getResourceMap(JP_Profesiones_Buscar.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(30, 30, 60, 14);

        TXT_Profesion.setText(resourceMap.getString("TXT_Profesion.text")); // NOI18N
        TXT_Profesion.setName("TXT_Profesion"); // NOI18N
        TXT_Profesion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TXT_ProfesionKeyPressed(evt);
            }
        });
        jPanel1.add(TXT_Profesion);
        TXT_Profesion.setBounds(90, 30, 220, 20);

        BTN_Buscar.setText(resourceMap.getString("BTN_Buscar.text")); // NOI18N
        BTN_Buscar.setName("BTN_Buscar"); // NOI18N
        BTN_Buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_BuscarActionPerformed(evt);
            }
        });
        jPanel1.add(BTN_Buscar);
        BTN_Buscar.setBounds(320, 30, 65, 23);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        TBL_Profesiones_Buscar.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Profesión", "Abreviatura"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TBL_Profesiones_Buscar.setName("TBL_Profesiones_Buscar"); // NOI18N
        TBL_Profesiones_Buscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TBL_Profesiones_BuscarMouseClicked(evt);
            }
        });
        TBL_Profesiones_Buscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TBL_Profesiones_BuscarKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(TBL_Profesiones_Buscar);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 60, 400, 220);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BTN_BuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_BuscarActionPerformed
//        String cadena = "select cod_profesion,descripcion,abreviatura from profesion ";
//        cadena = cadena + " where descripcion like '%"+TXT_Profesion.getText()+"%'";
//        Connection cn = Cls_Conexion.obtenerConexion();
//        try{
//            Statement st = cn.createStatement();
//            ResultSet rs = st.executeQuery(cadena);
//            String fila[];
//            DefaultTableModel mode = (DefaultTableModel)this.TBL_Profesiones_Buscar.getModel();
//            while(mode.getRowCount()>0){
//                mode.removeRow(0);
//            }
//            while(rs.next()){
//                fila = new String[3];
//                fila[0] = rs.getString("cod_profesion");
//                fila[1] = rs.getString("descripcion");
//                fila[2] = rs.getString("abreviatura");
//                mode.addRow(fila);
//            }
//            rs.close();
//            st.close();
//            cn.close();
//        }catch(Exception s){
//
//        }
        String fila[];
        DefaultTableModel mode = (DefaultTableModel) this.TBL_Profesiones_Buscar.getModel();
        String parametrosParaService_01[] = {Cls_Parametros.Map_FILTRO_NOMBRE, TXT_Profesion.getText()};
        ArrayList<Cls_Profesion> array_profesiones = Cls_InvocaWebService.obtenerTodasProfesiones(Cls_Funciones.generarHashMapParaSolicitud(parametrosParaService_01));
        while (mode.getRowCount() > 0) {
            mode.removeRow(0);
        }
        for (Cls_Profesion profesiones : array_profesiones) {
            fila = new String[3];
            fila[0] = profesiones.getCodigo();
            fila[1] = profesiones.getProfesion();
            fila[2] = profesiones.getAbreviatura();
            mode.addRow(fila);
        }
        this.TBL_Profesiones_Buscar.requestFocus();
    }//GEN-LAST:event_BTN_BuscarActionPerformed

    private void TBL_Profesiones_BuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TBL_Profesiones_BuscarKeyPressed
        if (evt.getKeyCode() == 10) { // tecla enter
            seleccionarFila();
        }
    }//GEN-LAST:event_TBL_Profesiones_BuscarKeyPressed

    private void TXT_ProfesionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXT_ProfesionKeyPressed
        if (evt.getKeyCode() == 10) { // tecla enter
            BTN_Buscar.requestFocus();
        }
    }//GEN-LAST:event_TXT_ProfesionKeyPressed

    private void TBL_Profesiones_BuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TBL_Profesiones_BuscarMouseClicked
        if (evt.getClickCount() == 2) {
            seleccionarFila();
        }
    }//GEN-LAST:event_TBL_Profesiones_BuscarMouseClicked
    private void seleccionarFila() {
        try {

            String codigo = "", profesion = "", abreviatura = "";
            codigo = (String) this.TBL_Profesiones_Buscar.getValueAt(this.TBL_Profesiones_Buscar.getSelectedRow(), 0);
            profesion = (String) this.TBL_Profesiones_Buscar.getValueAt(this.TBL_Profesiones_Buscar.getSelectedRow(), 1);
            abreviatura = (String) this.TBL_Profesiones_Buscar.getValueAt(this.TBL_Profesiones_Buscar.getSelectedRow(), 2);
            Cls_Profesion obj_profesion = new Cls_Profesion(codigo, profesion, abreviatura);
            if (this.getRefDep() instanceof JP_Profesiones_Mantenimiento) {
                ((JP_Profesiones_Mantenimiento) this.getRefDep()).obtenerProfesion(obj_profesion);
            } else {
                if (this.getRefDep() instanceof JP_Asignar_Profesion_Trabajador) {
                    ((JP_Asignar_Profesion_Trabajador) this.getRefDep()).obtenerProfesion(obj_profesion);
                }
            }
            this.cerrarPadre();
        } catch (ArrayIndexOutOfBoundsException e) {
//            e.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BTN_Buscar;
    private javax.swing.JTable TBL_Profesiones_Buscar;
    private javax.swing.JTextField TXT_Profesion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
